<?php
	// define the filename extensions you're allowing
	define('ALLOWED_FILENAMES', 'jpg|jpeg|gif|png');
	// define a directory the webserver can write to
	define('IMAGE_DIR', './upload');
	
	// check against a regexp for an actual http url and for a valid filename, also extract that filename using a submatch (see PHP's regexp docs to understand this)
	if(!preg_match('#^http://.*([^/]+\.('.ALLOWED_FILENAMES.'))$#', $_POST['img_url'], $m)) {
	  die('Invalid url given');
	}
	
	// try getting the image
	if(!$img = file_get_contents($_POST['img_url'])) {
	  die('Getting that file failed');
	}
	
	$filename = strrchr($m[0], "/");// En realidad es /filename
	// try writing the file with the original filename -- note that this will overwrite any existing filename in the same directory -- that's up to you to check for
	if(!$f = fopen(IMAGE_DIR.$filename, 'w')) {
	  die('Opening file for writing failed');
	}
	
	if (fwrite($f, $img) === FALSE) {
	  die('Could not write to the file');
	} 
	
	//echo "<img src="."upload" . $filename .">";
	fclose($f);
	$imagen = $filename;
	include 'insertarDB.php';
	include 'marquee.php';
?>